package com.hzncc.scaffold.controller.Rear.teachingClassroom.QuestionBank;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Qlibmajor;
import com.hzncc.scaffold.entity.Vo.CommitQuestionBankVo;
import com.hzncc.scaffold.entity.Vo.QuestionLibDetailVo;
import com.hzncc.scaffold.entity.Vo.QuestionLibIdVo;
import com.hzncc.scaffold.entity.Vo.UpdateQuestionBankVo;
import com.hzncc.scaffold.mapper.QuestionlibMapper;
import com.hzncc.scaffold.service.QlibmajorService;
import com.hzncc.scaffold.service.QuestionService;
import com.hzncc.scaffold.service.QuestionlibService;
import com.hzncc.scaffold.utils.NumberUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: DrunkBear
 * @Date: 2022-05-10 15:30
 * @Description: 题库
 */
@RestController
@Api(tags = "教学课堂管理-题库管理-题库")
@RequestMapping("/manage/questionLib")
public class QuestionBankController {

    @Autowired
    QuestionlibService questionlibService;

    @Autowired
    QuestionlibMapper questionlibMapper;

    @Autowired
    QuestionService questionService;

    @Autowired
    QlibmajorService qlibmajorService;

    @GetMapping("/list")
    @ApiOperation("题库列表")
    public ResultT list(Integer pageNum,Integer page, Integer pageSize, String keyword, Integer majorId) {
        Integer pageData = NumberUtils.pageNum(page, pageNum);
        ResultT resultT = questionlibService.listQuestionLib(pageData, pageSize, keyword, majorId);
        return resultT;
    }

    @PostMapping("/commit")
    @ApiOperation("新增题库")
    public ResultT commit(@RequestBody CommitQuestionBankVo commitQuestionBankVo) {

        List<String> titleList = questionlibMapper.getTitleList();
        if (CollUtil.contains(titleList, commitQuestionBankVo.getTitle())) {
            return ResultT.fail("已存在同名题库，不能重复添加");
        }

        ResultT resultT = questionlibService.commit(commitQuestionBankVo);

//        if (resultT.getCode() == 200) {
//            if (CollUtil.isNotEmpty(commitQuestionBankVo.getMajorId())) {
//                List<Integer> majorIdList = commitQuestionBankVo.getMajorId();
//                for (Integer majorId : majorIdList) {
//                    Qlibmajor qlibmajor = new Qlibmajor();
//                    qlibmajor.setMajorId(majorId);
//                    qlibmajor.setQuestionLibId((Integer) resultT.getData());
//                    qlibmajorService.save(qlibmajor);
//                }
//            }
//        }

        return resultT;
    }

    @PostMapping("/delete")
    @ApiOperation("删除题库")
    public ResultT delete(@RequestBody QuestionLibIdVo questionLibIdVo) {

        Integer questionLibId = questionLibIdVo.getQuestionLibId();

        ResultT resultT = questionlibService.deleteQuestionLib(questionLibId);

        if (resultT.getCode() == 200) {
            LambdaQueryWrapper<Qlibmajor> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Qlibmajor::getQuestionLibId, questionLibId);
            boolean remove = qlibmajorService.remove(lqw);
        }
        return resultT;
    }


    @PostMapping("/update")
    @ApiOperation("修改题库")
    public ResultT update(@RequestBody UpdateQuestionBankVo updateQuestionBankVo) {
        ResultT resultT = questionlibService.updateQuestionLib(updateQuestionBankVo);
        return resultT;
    }

//    @PostMapping("/detail")
//    @ApiOperation("维护题目")
//    public ResultT detail(Integer questionLibId, Integer type) {
//        ResultT resultT = questionlibService.detail(questionLibId, type);
//        return resultT;
//    }

    @GetMapping("/detail")
    @ApiOperation("维护题目")
    public ResultT detail(String questionLibId ) {
        ResultT resultT = questionlibService.detail(questionLibId);
        return resultT;
    }
}
